package world;

import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glTranslatef;
import graphics.Drawable;
import graphics.Tile;

import java.util.ArrayList;

/**
 * Holds the map and all the players in the game.
 * 
 * @author mstichel
 * 
 */

public class World extends Drawable {

	private Spawn blue, red;
	private Tile[][] tiles;
	ArrayList<Player> players = new ArrayList<Player>();

	public World() {
		init();
	}

	/**
	 * Load world file.
	 */
	private void init() {
		tiles = new Tile[128][128];
		for (int i = 0; i < tiles[0].length; i++) {
			for (int e = 0; e < tiles.length; e++) {
				if (i == 0 || e == 0 || i == tiles[0].length - 1 || e == tiles.length - 1) {
					tiles[i][e] = new Tile(Tile.Type.WALL);
				} else {
					tiles[i][e] = new Tile(Tile.Type.GRASS);
				}
			}
		}
		red = new Spawn(150f, 150f);
		blue = new Spawn(450f, 450f);
	}

	/**
	 * Spawn a player at the spawn-point of the players team.
	 * 
	 * @param p
	 *            The player to spawn.
	 */
	public void spawnPlayer(Player p) {
		switch (p.team) {
		case BLUE:
			blue.spawnPlayer(p);
			break;
		case RED:
			red.spawnPlayer(p);
			break;
		}
		players.add(p);
	}

	@Override
	public void draw() {
		for (int i = 0; i < tiles[0].length; i++) {
			for (int e = 0; e < tiles.length; e++) {
				glPushMatrix();
				{
					glTranslatef(i * Tile.TILE_SIZE, e * Tile.TILE_SIZE, 0f);
					tiles[i][e].draw();
				}
				glPopMatrix();
			}
		}
		for (Player p : players) {
			p.draw();
		}
	}
}
